/**
 * Created by Demon on 2015/4/25.
 */
var ElementPage = function () {

    var $car = $("#car");
    var $har = $("#har");
    var $oar = $("#oar");
    var $nar = $("#nar");
    var $sar = $("#sar");
    var $qnetAr = $("#qnetAr");
    var $qnetArOther = $("#qnetArOther");
    var $qgrAr = $("#qgrAr");
    var $qgrArOther = $("#qgrArOther");
    var $sum = $("#sum");

    function showBlockUI(message) {
        Metronic.blockUI({
            boxed: true,
            message: message
        });
        window.setTimeout(function () {
            Metronic.unblockUI();
        }, 500);
    }

    var updateCar = function () {
        $.ajax({
            url: basePath + "quality/element/updateCar",
            type: "post",
            dataType: "json",
            data: "car=" + $car.val(),
            success: function () {
                calculateSum();
            }
        })
    };

    var updateHar = function () {
        $.ajax({
            url: basePath + "quality/element/updateHar",
            type: "post",
            dataType: "json",
            data: "har=" + $har.val(),
            success: function () {
                calculateSum();
            }
        })
    };

    var updateOar = function () {
        $.ajax({
            url: basePath + "quality/element/updateOar",
            type: "post",
            dataType: "json",
            data: "oar=" + $oar.val(),
            success: function () {
                calculateSum();
            }
        })
    };

    var updateNar = function () {
        $.ajax({
            url: basePath + "quality/element/updateNar",
            type: "post",
            dataType: "json",
            data: "nar=" + $nar.val(),
            success: function () {
                calculateSum();
            }
        })
    };

    var updateSar = function () {
        $.ajax({
            url: basePath + "quality/element/updateSar",
            type: "post",
            dataType: "json",
            data: "sar=" + $sar.val(),
            success: function () {
                calculateSum();
            }
        })
    };

    var updateQnetAr = function () {
        $.ajax({
            url: basePath + "quality/element/updateQnetAr",
            type: "post",
            dataType: "json",
            data: "qnetAr=" + $qnetAr.val()
        })
    };

    var updateQnetArOther = function () {
        $.ajax({
            url: basePath + "quality/element/updateQnetArOther",
            type: "post",
            dataType: "json",
            data: "qnetArOther=" + $qnetArOther.val()
        });
    };

    var updateQgrAr = function () {
        $.ajax({
            url: basePath + "quality/element/updateQgrAr",
            type: "post",
            dataType: "json",
            data: "qgrAr=" + $qgrAr.val()
        });
    };

    var updateQgrArOther = function () {
        $.ajax({
            url: basePath + "quality/element/updateQgrArOther",
            type: "post",
            dataType: "json",
            data: "qgrArOther=" + $qgrArOther.val()
        });
    };

    var calculateSum = function () {
        if (validate.hasNotNumber([
            $car.val(),
            $har.val(),
            $oar.val(),
            $nar.val(),
            $sar.val()
        ]))return;

        showBlockUI("计算中..");

        $.ajax({
            url: "quality/element/calculateSum",
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#sum").val(data);
                $("#resultSum").html(data);
            }
        });

    };

    var calculateQnetArOther = function () {
        showBlockUI("计算中..");

        $.ajax({
            url: basePath + "quality/element/calculateQnetArOther",
            type: "post",
            dataType: "json",
            success: function (data) {
                $qnetArOther.val(data);
                $("#resultQnetAr").html($qnetAr.val());
                $("#resultQnetArOther").html(data);
                //$("#Formula1AndFormula2").hide();
            }
        })
    };

    var calculateQgrArOther = function () {
        showBlockUI("计算中..");

        $.ajax({
            url: basePath + "quality/element/calculateQgrArOther",
            type: "post",
            dataType: "json",
            success: function (data) {
                $qgrArOther.val(data);
                $("#resultQgrAr").html($qgrAr.val());
                $("#resultQgrArOther").html(data);
               // $("#Formula3AndFormula4").hide();
            }
        })
    };

    var calculateQnetArAndQnetArOther = function () {
        showBlockUI("计算中..");

        $.ajax({
            url: basePath + "quality/element/calculateQnetArAndQnetArOther",
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#qnetAr").val(data.returnData.qnetAr);
                $("#qnetArOther").val(data.returnData.qnetArOther);
                $("#resultQnetAr").html(data.returnData.qnetAr);
                $("#resultQnetArOther").html(data.returnData.qnetArOther);
                //$("#Formula1AndFormula2").hide();
            }
        });
    };

    var calculateQgrArAndQgrArOther = function () {
        showBlockUI("计算中..");

        $.ajax({
            url: basePath + "quality/element/calculateQgrArAndQgrArOther",
            type: "post",
            dataType: "json",
            success: function (data) {
                $qgrAr.val(data.returnData.qgrAr);
                $qgrArOther.val(data.returnData.qgrArOther);
                $("#resultQgrAr").html(data.returnData.qgrAr);
                $("#resultQgrArOther").html(data.returnData.qgrArOther);
               // $("#Formula3AndFormula4").hide();
            }
        });
    };


    return {
        init: function () {
            $car.on("blur", function () {
                validate.init($car);
                if (validate.isNotNumber($car.val())) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                updateCar();

            });

            $har.on("blur", function () {
                validate.init($har);
                if (validate.isNotNumber($har.val())) {
                    validate.invalid();
                    return;
                }
                validate.valid();

                updateHar();
            });

            $oar.on("blur", function () {
                validate.init($oar);
                if (validate.isNotNumber($oar.val())) {
                    validate.invalid();
                    return;
                }
                validate.valid();

                updateOar();
            });

            $nar.on("blur", function () {
                validate.init($nar);
                if (validate.isNotNumber($nar.val())) {
                    validate.invalid();
                    return;
                }
                validate.valid();

                updateNar();
            });

            $sar.on("blur", function () {
                validate.init($sar);
                if (validate.isNotNumber($sar.val())) {
                    validate.invalid();
                    return;
                }
                validate.valid();

                updateSar();
            });
            $("#qnetAr,#qnetArOther").on("blur",function(){
                var hasQnetAr = !validate.isNotNumber($qnetAr.val());
                var hasQnetArOther = !validate.isNotNumber($qnetArOther.val());
                if(hasQnetAr){
                    updateQnetAr();
                }
                if(hasQnetArOther){
                    updateQnetArOther();
                }

            });

            $("#Formula1AndFormula2").on("click", function () {
                var hasQnetAr = !validate.isNotNumber($qnetAr.val());
                var hasQnetArOther = !validate.isNotNumber($qnetArOther.val());
                if(!hasQnetAr && !hasQnetArOther) {
                    calculateQnetArAndQnetArOther();
                    return;
                }
                if(hasQnetAr) {
                    updateQnetAr();
                    calculateQnetArOther();
                }
            });

            $("#qgrAr,#qgrArOther").on("blur",function(){
                var hasQgrAr = !validate.isNotNumber($qgrAr.val());
                var hasQgrArOther = !validate.isNotNumber($qgrArOther.val());
                if(hasQgrAr){
                    updateQgrAr();
                }
                if(hasQgrArOther){
                    updateQgrArOther();
                }
            });
            $("#Formula3AndFormula4").on("click", function () {
                var hasQgrAr = !validate.isNotNumber($qgrAr.val());
                var hasQgrArOther = !validate.isNotNumber($qgrArOther.val());
                if(!hasQgrAr && !hasQgrArOther){
                    calculateQgrArAndQgrArOther();
                    return;
                }
                if(hasQgrAr) {
                    updateQgrAr();
                    calculateQgrArOther();
                }
            });

            $("#save").on("click", function () {
                window.location.href = basePath + "quality/element/save";
            });

        },

        recover: function() {
            $.get(basePath + "quality/element/recover",function(data){
                $car.val(getValidRecoverVal(data.car));
                $har.val(getValidRecoverVal(data.har));
                $oar.val(getValidRecoverVal(data.oar));
                $nar.val(getValidRecoverVal(data.nar));
                $sar.val(getValidRecoverVal(data.sar));
                $qnetAr.val(getValidRecoverVal(data.qnetAr));
                $qnetArOther.val(getValidRecoverVal(data.qnetArOther));
                $qgrAr.val(getValidRecoverVal(data.qgrAr));
                $qgrArOther.val(getValidRecoverVal(data.qgrArOther));
                $sum.val(getValidRecoverVal(data.sum));
                $("#resultQgrAr").html(data.qgrAr);
                $("#resultQgrArOther").html(data.qgrArOther);
                $("#resultQnetAr").html(data.qnetAr);
                $("#resultQnetArOther").html(data.qnetArOther);
                $("#resultSum").html(data.sum);
                var hasQnetAr = !validate.isNotNumber($qnetAr.val());
                var hasQnetArOther = !validate.isNotNumber($qnetArOther.val());
                var hasQgrAr = !validate.isNotNumber($qgrAr.val());
                var hasQgrArOther = !validate.isNotNumber($qgrArOther.val());

            });
        }
    }
}();